{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python37764bit5d211478a4cf4a3d93a2c891f6a9207d",
   "display_name": "Python 3.7.7 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "&#39;aaa&#39;"
     },
     "metadata": {},
     "execution_count": 1
    }
   ],
   "source": [
    "'''\n",
    "列表也可以用作队列，其中先添加的元素被最先取出 (“先进先出”)；然而列表用作这个目的相当低效。因为在列表的末尾添加和弹出元素非常快，但是在列表的开头插入或弹出元素却很慢 (因为所有的其他元素都必须移动一位)。若要实现一个队列，可使用 collections.deque，它被设计成可以快速地从两端添加或弹出元素。例如\n",
    "'''\n",
    "\n",
    "from collections import deque\n",
    "queue = deque(['aaa','bbb','ccc'])\n",
    "queue.append('ddd')\n",
    "queue.append('eee')\n",
    "queue.popleft()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "&#39;bbb&#39;"
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "queue.popleft()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "deque([&#39;ccc&#39;, &#39;ddd&#39;, &#39;eee&#39;])"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "queue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[1, 2, 3, 4, 5]"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "'''\n",
    "为什么不能用reverse()，just try it!\n",
    "'''\n",
    "lst = [1, 2, 3, 4]\n",
    "\n",
    "def que(lst):\n",
    "    lst.reverse()\n",
    "    res = lst.pop()\n",
    "    lst.reverse()\n",
    "    return res\n",
    "\n",
    "lst.append(5)\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "1"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "que(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "2"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "que(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[3, 4, 5]"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}